package cn.cvs.servlet.sysRole;

import cn.cvs.service.SysRoleService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "CodeExistServlet", urlPatterns = "/sys/role/codeExist")
public class CodeExistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        PrintWriter writer = response.getWriter ( );
        SysRoleService sysRoleService= (SysRoleService) SpringUtil.getBean ( "sysRoleService" );
        Map map = new HashMap<> ( );
        if (request.getParameter ( "code" ) != null && !"".equals ( request.getParameter ( "code" ).trim ( ) )) {
            String code = request.getParameter ( "code" );
            if (sysRoleService.checkCodeExist ( code ) != null) {
                map.put ( "exist", 1 );
            } else {
                map.put ( "exist", 0 );
            }
        } else {
            map.put ( "exist", -1 );
            map.put ( "msg", "编号不能为空" );
        }
        writer.write ( JSON.toJSONString ( map ) );
        writer.close ();
    }
}
